package com.itheima.test6;

public class StudentTest1 {
    public static void main(String[] args) {
        Student[] arr = new Student[3];

        Student st1 = new Student(1,"展示",20);
        Student st2 = new Student(2,"歷史",21);
        Student st3 = new Student(3,"外网",24);

        arr[0] = st1;
        arr[1] = st2;
        arr[2] = st3;

        //需求：通过id删除学生信息
        //1.获取索引
        int index = getIndex(arr,2);
        if (index == -1){
            System.out.println("该学生不存在,删除失败");
        }else {
            arr[index] = null;
            print(arr);
        }
    }

    public static void print(Student[] arr){
        for (int i = 0; i < arr.length; i++) {
            if (arr[i] != null){
                Student st = arr[i];
                System.out.println(st.getId() + "," + st.getName() + "," + st.getAge());
            }
        }
    }
    public static int getIndex(Student[] arr,int id){
        int index = -1;
        for (int i = 0; i < arr.length; i++) {
            if (arr[i] != null){
                Student st = arr[i];
                if (st.getId() == id){
                    index = i;
                    break;
                }
            }
        }

        return index;
    }

}
